package view
{
	import controller.ItemFactory;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import model.Item;

	public class Bag extends Sprite
	{
		//用来加载背景图片
		private var loader:Loader=new Loader();
		private var info:TextField;
		
		public function Bag()
		{
			this.init();
			this.addItem();
		}
		
		/**
		 * 添加物品到包中
		 */
		public function addItem():void{
			//获得物品列表
			var itemList:Array=new ItemFactory().getItemList();
			var length:uint=itemList.length;
			//设置物品显示位置
			for(var i:uint=0;i<length;i++){
				var item:Item=itemList[i] as Item;
				info=new TextField();
				info.textColor=0xFFFFFF;
				info.text=int(Math.random()*10).toString();
				if(null!=item){
					item.x=14+(i%6)*36;
					item.y=51+uint(i/6)*36;
					info.x=item.x+23;
					info.y=item.y+20;
					this.addChild(item);
					this.addChild(info);
				}
				
			}
		}
		
		/**
		 * 初始化背包背景，拖拽动作
		 */
		private function init():void{
			var url:URLRequest=new URLRequest("bg.png");
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onloadedBackground);
			loader.load(url);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
			//this.mouseChildren=false;//使子画面不接受此鼠标动作
		}
		/**
		 * 添加背景图片
		 */
		private function onloadedBackground(e:Event):void{
			var image:Bitmap;image=Bitmap(loader.content);
			this.addChild(image);
			setChildIndex(image,0);//设置背景图片显示在最			
		}
		
		/**
		 * 鼠标点下，判断点击区域，为标题栏区域添加拖拽行为
		 */
		private function mouseDownHandler(e:MouseEvent):void{
			//点击到标题栏区域才拖动
			if(mouseX>0&&mouseX<242&&mouseY>0&&mouseY<17){
				this.startDrag();
			}
			
		}
		/**
		 * 鼠标放开
		 */
		private function mouseUpHandler(e:MouseEvent):void{
			//点击到标题栏区域才拖动
			if(mouseX>0&&mouseX<242&&mouseY>0&&mouseY<17){
				this.stopDrag();
			}
		}
	}
}